package com.wf.course.web.controller.goods;

import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.utils.page.PageBean;
import com.wf.course.model.goods.GoodsType;
import com.wf.course.service.IGoodsTypeService;
import com.wf.course.web.Interceptor.LogInfo;
/**
 * 
 * @content 列表Controller
 */
@RequestMapping("goodsType")
@Controller
public class GoodsTypeController {

	@Autowired
	private IGoodsTypeService goodsTypeService;

	/**
	 * 列表展示
	 * @param pageNo
	 * @param pageSize
	 * @param typeName
	 * @param model
	 * @return
	 */
	@LogInfo("查询商品分类信息列表")
	@RequestMapping("goodsTypeList")
	public String goodsTypelist(@RequestParam(value = "pageNo", required = false) Integer pageNo,
			@RequestParam(value = "pageSize", required = false) Integer pageSize,
			@RequestParam(value = "typeName", required = false) String typeName, 
			Model model) {

		// 组装一个查询条件的map
		Map<String, Object> cons = new HashMap<String, Object>();
		cons.put("typeName", typeName);
		PageBean<GoodsType> pageBean = new PageBean<GoodsType>(pageNo, pageSize, null, cons);
		pageBean.setUrl("goodsType/goodsTypeList");
		this.goodsTypeService.indexInfo(pageBean);
		
		model.addAttribute("page", pageBean);
		return "goods/goodsTypeList";
	}

	/**
	 *  修改和新增页面数据初始化
	 * 
	 * @param id
	 * @param model
	 * @return
	 */
	@LogInfo("新增或修改商品分类信息,页面数据初始化")
	@RequestMapping("pageLoad/{id}")
	public String downLoadById(@PathVariable("id") Integer id, Model model) {
		 
		if (id != null && id != 0) {
			GoodsType goodsType = goodsTypeService.queryGoodsTypeById(id);
			model.addAttribute("goodsType", goodsType);
			model.addAttribute("type", "upd");
		}else{
			model.addAttribute("type", "add");
		}
		 
		
		return "goods/goodsTypeEdit";
	}

	/**
	 * 新增和修改
	 * @param goodsType
	 * @param request
	 * @return
	 */
	@LogInfo("新增或修改商品分类信息")
	@RequestMapping(value = "goodsTypeEdit" , produces = "text/html;charset=UTF-8")
	public String goodsTypeEdit(@ModelAttribute GoodsType goodsType, HttpServletRequest request) {
		
		goodsTypeService.saveOrUpdateGoodsType(goodsType);
		
		return "redirect:/goodsType/goodsTypeList";
	}

	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@LogInfo("删除商品分类信息")
	@RequestMapping(value = "remove/{id}")
	public String removeGoodsType(@PathVariable("id") Integer id) {
		
		goodsTypeService.deleteById(id);
		
		return "redirect:/goodsType/goodsTypeList";
	}
	

}
